package com.qin.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.mall.domain.UmsResource;
import com.qin.mall.domain.UmsRole;
import com.qin.mall.domain.UmsRoleResourceRelation;
import com.qin.mall.mapper.UmsResourceMapper;
import com.qin.mall.mapper.UmsRoleMapper;
import com.qin.mall.common.constant.AuthConstant;
import com.qin.mall.common.service.RedisService;
import com.qin.mall.mapper.UmsRoleResourceRelationMapper;
import com.qin.mall.service.UmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author 覃剑
* @description 针对表【ums_resource(后台资源表)】的数据库操作Service实现
* @createDate 2022-11-14 18:58:32
*/
@Service
public class UmsResourceServiceImpl extends ServiceImpl<UmsResourceMapper, UmsResource>
    implements UmsResourceService{
    
    @Autowired
    private UmsResourceMapper resourceMapper;
    @Autowired
    private UmsRoleMapper roleMapper;
    @Autowired
    private UmsRoleResourceRelationMapper roleResourceRelationMapper;
    @Value("user-manage")
    private String applicationName;
    @Autowired
    private RedisService redisService;

    /**
     * 添加资源
     */
    @Override
    public int create(UmsResource umsResource) {
        umsResource.setCreateTime(new Date());
        int count = resourceMapper.insert(umsResource);
        initResourceRolesMap();
        return count;
    }

    /**
     * 修改资源
     */
    @Override
    public int update(Long id, UmsResource umsResource) {
        umsResource.setId(id);
        int count = resourceMapper.updateById(umsResource);
        initResourceRolesMap();
        return count;
    }

    /**
     * 获取资源详情
     */
    @Override
    public UmsResource getItem(Long id) {
        UmsResource resource = resourceMapper.selectById(id);
        return resource;
    }

    /**
     * 删除资源
     */
    @Override
    public int delete(Long id) {
        int count = resourceMapper.deleteById(id);
        return count;
    }

    /**
     * 分页查询资源
     */
    @Override
    public Page<UmsResource> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        Page<UmsResource> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UmsResource> wrapper = new QueryWrapper<>();
        if (categoryId!=null){
            wrapper.eq("category_id",categoryId);
        }
        if (StrUtil.isNotEmpty(nameKeyword)){
            wrapper.like("name",nameKeyword);
        }
        if (StrUtil.isNotEmpty(urlKeyword)){
            wrapper.like("url",urlKeyword);
        }
        Page<UmsResource> selectPage = resourceMapper.selectPage(page, wrapper);
        return selectPage;
    }

    /**
     * 查询全部资源
     */
    @Override
    public List<UmsResource> listAll() {
        List<UmsResource> umsResources = resourceMapper.selectList(null);
        return umsResources;
    }

    @Override
    public Map<String, List<String>> initResourceRolesMap() {
        Map<String,List<String>> resourceRoleMap = new TreeMap<>();
        List<UmsResource> resourceList = resourceMapper.selectList(null);
        List<UmsRole> roleList = roleMapper.selectList(null);
        List<UmsRoleResourceRelation> relationList = roleResourceRelationMapper.selectList(null);
        for (UmsResource resource : resourceList) {
            Set<Long> roleIds = relationList.stream().filter(item -> item.getResourceId().equals(resource.getId())).map(UmsRoleResourceRelation::getRoleId).collect(Collectors.toSet());
            List<String> roleNames = roleList.stream().filter(item -> roleIds.contains(item.getId())).map(item -> item.getId() + "_" + item.getName()).collect(Collectors.toList());
            resourceRoleMap.put("/"+applicationName+resource.getUrl(),roleNames);
        }
        redisService.del(AuthConstant.RESOURCE_ROLES_MAP_KEY);
        redisService.hSetAll(AuthConstant.RESOURCE_ROLES_MAP_KEY, resourceRoleMap);
        return resourceRoleMap;
    }
}




